package cn.xorange.commons.utils.exception;

import cn.xorange.commons.utils.lang.ObjectUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * @author : yangjian
 * date : 2023/11/23
 * description :
 */
@Slf4j
public class ExceptionUtils {
    ExceptionUtils(){}

    private static final String LINE_SEPARATOR = "line.separator";

    public static String getCauseMsg(Throwable throwable){
        String causeMessage = throwable.getMessage();
        if(Objects.isNull(causeMessage)){
            return "java.lang.NullPointerException";
        }else {
            StringBuilder sb = new StringBuilder();
            String[] array = causeMessage.split("[\\r\\n]");
            if(array.length==1){
                sb.append(array[0]);
            }
            for (String str : array) {
                String[] ms = str.split(";");
                if (str.contains("### Cause:") && ms.length > 0) {
                    sb.append(System.getProperty(LINE_SEPARATOR));
                    break;
                } else if (str.contains("error in your SQL")&& ms.length > 0) {
                    sb.append(ms[ms.length - 1]);
                    sb.append(System.getProperty(LINE_SEPARATOR));
                }
            }
            for (String str : array) {
                if (str.contains("### The error may involve") ) {
                    sb.append(str);
                    sb.append(System.getProperty(LINE_SEPARATOR));
                }
                if (str.contains("### SQL:")) {
                    sb.append(str);
                }
            }
            return sb.toString();
        }
    }


    public static void printLog(BaseException baseException){
        // 打印错误日志
        StringBuilder logSb = new StringBuilder();
        if (!ObjectUtils.isEmpty(baseException.getStackTrace())){
            StackTraceElement stackTrace = baseException.getStackTrace()[0];
            logSb.append(baseException.getMessage());
            if(!ObjectUtils.isEmpty(baseException.getBecause())){
                logSb.append(" ");
                logSb.append(baseException.getBecause());
                logSb.append(System.getProperty(LINE_SEPARATOR));
                logSb.append("                     ");
                logSb.append(stackTrace.getClassName());
                logSb.append(".");
                logSb.append(stackTrace.getMethodName());
                logSb.append("()(");
                logSb.append(stackTrace.getFileName());
                logSb.append(":");
                logSb.append(stackTrace.getLineNumber());
                logSb.append(")");
            }
        }else {
            logSb.append(baseException.getMessage());
            if(!baseException.getBecause().isEmpty()){
                logSb.append(System.getProperty(LINE_SEPARATOR));
                logSb.append(baseException.getBecause());
            }
            if(!baseException.getBecause().isEmpty()){
                logSb.append(System.getProperty(LINE_SEPARATOR));
                logSb.append("                     ");
            }
        }
        log.error(logSb.toString());
    }
}
